/**
 * Project: Useful Programs
 * Package: tools
 */
package org.leumasjaffe.collections;

import java.util.List;
import java.util.Random;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
 * Class Shuffle in file Shuffle.java
 * 
 * @author leumasjaffe
 * Created on: Jul 26, 2012
 * 
 */
@NonNullByDefault
public final class Shuffle {
	
	private static final Random rand = new Random();
	
	private Shuffle(){}
	
	/**
	 * @param toShuffle
	 */
	public static <T> void shuffle(T[] toShuffle, Random rand) {
		int len = toShuffle.length, roll;
		for (int slot = 0; slot < len; slot++) {
			roll = rand.nextInt(len - slot) + slot;
			Swap.swap(toShuffle, slot, roll);
		}
	}
	
	/**
	 * @param toShuffle
	 * @deprecated Use {@link java.util.Collections#shuffle(List, Random)}
	 */
	@Deprecated
	public static <T> void shuffle(List<T> toShuffle, Random rand) {
		int len = toShuffle.size(), roll;
		for (int slot = 0; slot < len; slot++) {
			roll = rand.nextInt(len - slot) + slot;
			Swap.swap(toShuffle, slot, roll);
		}
	}
	
	/**
	 * @param toShuffle
	 */
	public static <T> void shuffle(T[] toShuffle) {
		shuffle(toShuffle, rand);
	}
	
	/**
	 * @param toShuffle
	 * @deprecated Use {@link java.util.Collections#shuffle(List)}
	 */
	@Deprecated
	public static <T> void shuffle(List<T> toShuffle) {
		shuffle(toShuffle, rand);
	}

}
